//
//  KZMessageVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/27.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZMessageVC: KZBaseViewControllerWithBack {
    
    let segmentBar = KZSegmentView()
    let scroll = UIScrollView()
    let messageOfMineView = KZMessageOfMineView()
    let messageOfSystemView = KZMessageOfSystemView()
    
    var childIndexs = [Int]()

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "消息通知"
        
        setupUI()
        
        
    }
    
    override func viewSafeAreaInsetsDidChange() {
        if #available(iOS 11.0, *) {
            super.viewSafeAreaInsetsDidChange()
            scroll.snp.updateConstraints { (m) in
                m.bottom.equalTo(-view.safeAreaInsets.bottom)
            }
        }
    }


}

// MARK: ------ 布局
extension KZMessageVC {
    
    func setupUI() {
        
        // 搜索
        view.addSubview(segmentBar)
        segmentBar.config.backgroundColor = UIColor.white
        segmentBar.config.titleNormalColor = UIColor.lightGray
        segmentBar.config.titleSelectColor = kThemeColor
        segmentBar.config.indicatorColor = kThemeColor
        segmentBar.config.indicatorHeight = 2
        segmentBar.titles = ["我的消息", "系统消息"]
        segmentBar.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.top.equalTo(0)
            m.height.equalTo(44)
        }
        segmentBar.segmentClickHandler = { [weak self] in
            
            self!.scroll.setContentOffset(CGPoint(x: $0.cgFloat * self!.scroll.width, y: 0), animated: true)
        }
        
        view.addSubview(scroll)
        scroll.delegate = self
        scroll.isPagingEnabled = true
        scroll.showsVerticalScrollIndicator = false
        scroll.showsHorizontalScrollIndicator = false
        scroll.snp.makeConstraints { (m) in
            m.left.right.bottom.equalTo(0)
            m.top.equalTo(segmentBar.snp.bottom)
        }
        
        scroll.contentSize.width = kScreenWidth * 2
        
        
        scroll.addSubview(messageOfMineView)
        messageOfMineView.snp.makeConstraints { (m) in
            m.top.left.width.height.equalTo(scroll)
        }
        
        
        scroll.addSubview(messageOfSystemView)
        messageOfSystemView.snp.makeConstraints { (m) in
            m.top.width.height.equalTo(scroll)
            m.left.equalTo(messageOfMineView.snp.right)
        }
        
        self.messageOfMineView.items = [["", ""], ["", "", ""]]
        self.messageOfSystemView.items = ["", ""]
        
        _ = delay(deadline: 3, execute: {
            self.messageOfMineView.items = [[], []]
            self.messageOfSystemView.items = []
        })
        

        
        
    }
    

    
}


extension KZMessageVC: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let index = floor(scrollView.contentOffset.x / scrollView.width).int
        segmentBar.selectIndex(index: index)
    }
}
